package cn.jcby.app.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author: lihaotian
 * @since: 2023/12/7 17:31
 * @description
 **/
@Component
public class JWTSUtils {
    //jwt 密钥、有效期
    public String generateJwtToken(String subject){
        Date now = new Date();
        return "Bearer "+Jwts.builder()
                .setSubject(subject)
                .setIssuedAt(now)
                .setExpiration(new Date(now.getTime()+ConstantKey.MILLISECOND))
                .signWith(SignatureAlgorithm.HS256,ConstantKey.SIGNING_KEY).compact();
    }

    //解析token
    public Claims parseJwtToken(String token){
        return Jwts.parser()
                .setSigningKey(ConstantKey.SIGNING_KEY)
                .parseClaimsJws(token)
                .getBody();
    }
}
